package com.catt.orderdinner.controller;

import com.catt.orderdinner.common.ExcelData;
import com.catt.orderdinner.common.ExportExcelUtils;
import com.catt.orderdinner.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("admin/order")
public class AdminOrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("todayList")
    public String getTodayOrderList(@RequestParam(value = "lunchPage", defaultValue = "1") Integer lunchPage,
                              @RequestParam(value = "lunchSize", defaultValue = "10") Integer lunchSize,
                                    @RequestParam(value = "dinnerPage", defaultValue = "1") Integer dinnerPage,
                                    @RequestParam(value = "dinnerSize", defaultValue = "10") Integer dinnerSize,Model model) throws ParseException {
        PageRequest lunchRequest = new PageRequest(lunchPage - 1, lunchSize);
        PageRequest dinnerRequest = new PageRequest(dinnerPage - 1, dinnerSize);
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());

        Date lunchStar = formatter.parse(today+" 09:00");
        Date lunchEnd = formatter.parse(today+" 11:30");
        Date dinnerStar = formatter.parse(today+" 14:30");
        Date dinnerEnd = formatter.parse(today+" 17:30");
        Page<List<Map>> lunchOrder = orderService.getNowOrder(lunchStar,lunchEnd,lunchRequest);
        Page<List<Map>> dinnerOrder = orderService.getNowOrder(dinnerStar,dinnerEnd,dinnerRequest);
        model.addAttribute("pageUrl","/admin/order/list");
        model.addAttribute("lunchCurrentPage",lunchPage);
        model.addAttribute("lunchPageInfo",lunchOrder);
        model.addAttribute("lunchSize",lunchSize);
        model.addAttribute("dinnerCurrentPage",dinnerPage);
        model.addAttribute("dinnerPageInfo",dinnerOrder);
        model.addAttribute("dinnerSize",dinnerSize);
        return "order/todayOrderList";
    }

    @RequestMapping("monthlyList")
    public String getMonthlyList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "size", defaultValue = "10") Integer size,
                                 @RequestParam(value = "monthly", defaultValue = "") String monthly,Model model) throws ParseException {
        PageRequest request = new PageRequest(page - 1, size);
        SimpleDateFormat yearYormatter = new SimpleDateFormat("yyyy");
        SimpleDateFormat monthlyYormatter = new SimpleDateFormat("yyyy-MM");
        if(StringUtils.isEmpty(monthly)){
            monthly = monthlyYormatter.format(new Date())+"%";
        }else{
            monthly = yearYormatter.format(new Date())+"-"+monthly+"%";
        }
        Page<List<Map>> monthlyList = orderService.getMonthlyOrder(monthly,request);
        model.addAttribute("pageUrl","/admin/order/monthlyOrderList");
        model.addAttribute("currentPage",page);
        model.addAttribute("pageInfo",monthlyList);
        model.addAttribute("size",size);
        return "order/monthlyOrderList";
    }

    @RequestMapping("excel")
    public void excel(HttpServletResponse response,
                      @RequestParam(value = "monthly", defaultValue = "") String monthly) throws Exception{
        SimpleDateFormat yearYormatter = new SimpleDateFormat("yyyy");
        SimpleDateFormat monthlyYormatter = new SimpleDateFormat("yyyy-MM");
        if(StringUtils.isEmpty(monthly)){
            monthly = monthlyYormatter.format(new Date())+"%";
        }else{
            monthly = yearYormatter.format(new Date())+"-"+monthly+"%";
        }
        String exprotDate = monthly.replace("%","");
        ExcelData data = new ExcelData();
        data.setName(exprotDate+"月度加班餐报表");
        List<String> titles = new ArrayList();
        titles.add("项目编号");
        titles.add("项目名称");
        titles.add("项目经理");
        titles.add("总额(元)");
        data.setTitles(titles);
        List<Map> monthlyList = orderService.getMonthlyOrder(monthly);
        List<List<Object>> rows = new ArrayList();
        List<Object> row = new ArrayList();
        for (Map map : monthlyList){
            String projectType = (String) map.get("PROJECT_TYPE");
            String projectName = (String) map.get("PROJECT_NAME");
            String projectManager = (String) map.get("PROJECT_MANAGER");
            int amount = Integer.parseInt(map.get("AMOUNT").toString());
            row.add(projectType);
            row.add(projectName);
            row.add(projectManager);
            row.add(amount);
            rows.add(row);
        }
        data.setRows(rows);
        ExportExcelUtils.exportExcel(response,exprotDate+"月度加班餐报表.xlsx",data);
    }

}
